﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;

/// <summary>
/// Summary description for DAL_NguoiDung
/// </summary>
public class DAL_NguoiDung : SqlConnectionRun
{
	public DAL_NguoiDung()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public NguoiDung DangNhap(string MaNV, string MatKhau)
    {        
        string strSel = @"Select MaNV,HoTen,MatKhau,k.makhoa,k.tenkhoa/*,chukydientu */from HanhChanhBV..nguoidung 
            left join vienphi..dm_khoa k on nguoidung.makhoa=k.makhoa where MaNV='" + MaNV + "' and MatKhau='" + MatKhau + "' ";
        DataTable dt = SqlExecuteQueryReader(strSel);       
        if (dt.Rows.Count > 0)
        {
            NguoiDung NguoiDung = new NguoiDung();
            foreach (DataRow row in dt.Rows)
            {
                NguoiDung.MaNV = row["MaNV"].ToString().Trim();
                NguoiDung.MatKhau = row["MatKhau"].ToString();
                NguoiDung.HoTen = row["HoTen"].ToString().Trim();
                NguoiDung.MaKhoa = row["MaKhoa"].ToString().Trim();
                NguoiDung.TenKhoa = row["TenKhoa"].ToString().Trim();
            }
            return NguoiDung;
        }
        else 
            return null;
    }

    public void LuuVaoLog(string MaNV, string HoTen)
    {
        string ngay = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string strSel = "insert into HanhChanhBV..tblogin_log(username,fullname,module_name,date_in) values(N'" + MaNV + "',N'" + HoTen + "','kham','" + ngay + "')";
        SqlExecuteNoneQuery(strSel);
    }

    public string LayToanBoQuyen(string _userName)
    {
        string temp = "";
        string sqlselect = "select MaNV, MaNhom, MaQuyen from HanhChanhBV..PhanQuyen where manv='" + _userName + "'";
        DataTable dt = SqlExecuteQueryReader(sqlselect);
        foreach(DataRow row in dt.Rows)
        {
            temp += row["MaNV"].ToString().Trim() + " " + row["MaNhom"].ToString().Trim()
                    + " " + row["MaQuyen"].ToString().Trim() + " ";
        }      
        return temp;
    }

    public bool KiemTraQuyen(Page page, string manhomvao, string maquyenvao)
    {
        string manv = "";
        string matkhau = "";
        HttpSessionState Session = page.Session;
        if (Session["mand"] != null)
            manv = Session["mand"].ToString();
        if (Session["matkhau"] != null)
            matkhau = Session["matkhau"].ToString();
        string strsel = @"select n.MaNV,MaNhom,MaQuyen 
                        from HanhChanhBV..nguoidung as n, HanhChanhBV..phanquyen as p 
                        where n.MaNV=p.MaNV and n.MaNV=N'" + manv + "' and n.MatKhau=N'" + matkhau + "'";
        DataTable dt = SqlExecuteQueryReader(strsel);
        if (dt.Rows.Count > 0)
        {
            foreach(DataRow row in dt.Rows)
            {
                if (maquyenvao == row["MaQuyen"].ToString().Trim() || manhomvao == row["MaNhom"].ToString().Trim())
                {                  
                    return true;
                }
            }           
        }      
        return false;
    }


}